package com.cheng.chengliving.commodity.web;

import com.cheng.chengliving.commodity.entity.CategoryEntity;
import com.cheng.chengliving.commodity.service.CategoryService;
import com.cheng.chengliving.commodity.vo.Catalog2Vo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: 朱程
 * @Date: 2023/02/21/19:26
 * @Description:
 */

@Controller
public class IndexController {

    //装配CategoryService
    @Resource
    private CategoryService categoryService;

    //响应用户请求首页 http://localhost:9090 或 http://localhost:9090/index.html
    @GetMapping(value = {"/","/index.html"})
    private String indexPage(Model model) {

        //获取所有一级分类
        List<CategoryEntity> categoryEntities =
                categoryService.getLevel1Categories();
        //将categoryEntities放入到model
        model.addAttribute("categories",categoryEntities);

        System.out.println("categoryEntities->"+categoryEntities);
        //默认找classpath\templates\index.html
        return "index";
    }

    //测试-返回json数据
    @GetMapping(value = "/index/catalog.json")
    @ResponseBody
    public Map<String,List<Catalog2Vo>> getCatalogJson() {

        Map<String, List<Catalog2Vo>> catalogJson =
                categoryService.getCatalogJson();

        return catalogJson;
    }
}
